This notebook showcases the custom package NaturalUnits, which performs unit conversions in terms of the natural units frequently used  in high energy physics. It is supplementary material for Eemeli Tomberg, “Unit conversions and collected numbers in cosmology,” https://arxiv.org/abs/2110.12251.
Further information and examples can be found at http://www.eemelitomberg.net/NaturalUnits.

NaturalUnits: A package for unit conversions with natural unit systems

Eemeli Tomberg

NaturalUnits is a package for working with the natural unit systems common in high energy physics, defined by setting a number of physical constants to one. This often simplifies the math and lets one compare quantities with different SI units in a ‘natural’ way. The package lets the user set up the preferred natural unit system and perform conversions between dimensionful quantities within that system. The package is designed to be highly customizable: it is easy to define new units and unit systems. Commonly used units, physical constants, and unit systems are built in.

Let’s start by loading the package. This can be done, for example, by placing NaturalUnits.m into the notebook’s directory and running the code below. By default, full SI units are in use instead of a natural unit system.

SetDirectory[NotebookDirectory[]];
<<"NaturalUnits`"

Alternatively, it’s possible to load the package directly from the web:

Import["http://www.eemelitomberg.net/codes/NaturalUnits/v2/NaturalUnits.m"]

Unit conversions

The most important function of the package is NaturalConvert, converting the first argument to the units given by the second argument.

NaturalConvert[3.3ly,km]

Units are represented as common Mathematica symbols. They can feature in any combination in the first and second argument of NaturalConvert, as long as the combination has a unique dimension in the current unit system. All built-in units are documented in the package:

?ly

?km

NaturalConvert[3.3ly-pc,km^2/m]

Leaving the second argument out reverts to the unit given by the option ToUnitDefault, or to base units if ToUnitDefault is set to NoUnit (its default value).

NaturalConvert[3.3ly]

SetOptions[NaturalConvert,ToUnitDefault->cm];
NaturalConvert[{3.3ly,pc,2mm}]

As demonstrated above, if the first argument is a list, all its members are converted separately.

SetOptions[NaturalConvert,ToUnitDefault->NoUnit];
NaturalConvert[{3.3ly,pc,2mm}]

If the units don’t match, the package tries to correct the power of the target unit.

NaturalConvert[6.2b,1/m]

NaturalConvert[6.2b,s]

The second argument can also be a list of units {x, y, z, ...}, in which case the target unit is formed out of these with suitable exponents, (x^(Subscript[n, 1]))(y^(Subscript[n, 2]))(z^(Subscript[n, 3]))..., if a unique form can be found.

NaturalConvert[MEarth/(4/3*Pi*(6370km)^3),{cm,g}]

The target unit can be dropped from the final result, or kept in a non-expanded form by setting the option KeepUnit.

NaturalConvert[1AU/c,min]

NaturalConvert[1AU/c,min,KeepUnit->False]

NaturalConvert[1AU/c,2*Pi*6370km/(343m/s),KeepUnit->Defer]

Setting up a natural unit system

Setting up unit systems

A new unit system, given as a set of equations for units and physical constants, together with a set of natural units, can be set using NaturalUnitsSetup.

NaturalUnitsSetup[NewUnitSystem->{hbar==1,c==1}, NewNatUnits->{eV,e,K,deg}]

In the current unit system, length, time, and mass share the same units.

NaturalConvert[5fm]

NaturalConvert[s,m]

NaturalConvert[mProton,GeV]

If the natural base units aren’t set fully, the package automatically completes the list.

NaturalUnitsSetup[NewUnitSystem->{c==1,G==1},NewNatUnits->{ly}]
$NatUnits

A number of unit systems have been predefined.

$NaturalUnitSystemList

?PlanckUnits

NaturalUnitsSetup[NewUnitSystem->PlanckUnits]

NaturalConvert[{mP,lP,TP,tP}]

NaturalConvert[mProton]

NaturalUnitsSetup[NewUnitSystem->{}]

Adding new units

New units can be added with NaturalUnitsAdd, which takes in a transformation rule from a new unit to other units. The options store the unit’s definition (for documentation purposes) and make the unit take SI prefixes. Overlapping old definitions are rewritten.

NaturalUnitsAdd[mi->1.609344km,UnitDefinition->"mile",Prefixable->True]

NaturalConvert[mi/h,m/s]

NaturalConvert[kmi,mi]

Miscellaneous

Getting information about the units

NaturalUnitsCheck gives basic information on a unit.

NaturalUnitsCheck[AU]

NaturalUnitsCheck[s]

NaturalUnitsList lists all (non-prefixed) units.

NaturalUnitsList[]

Modifying prefixes

Metric prefixes, together with the numerical factors, are stored in $PrefixTable. All units that take prefixes are stored in $PrefixableUnits. These can be adjusted by hand; NaturalUnitsSetup[] should be called afterwards to update the transformation rules. The number of pregenerated units and prefixes has been limited to not overload the namespace.

$PrefixTable
$PrefixableUnits

?ah

AppendTo[$PrefixTable,"a"->10^-18];
AppendTo[$PrefixableUnits,h];
NaturalUnitsSetup[];
?ah

NaturalConvert[ah,s]

Setting  a display style

NaturalUnitsSetStyle sets the style of display for the unit symbols. This feature may be unstable and is turned off by default.

NaturalUnitsSetStyle[Red]
NaturalConvert[ly^2,m^2]

Calling the function without arguments resets the style.

NaturalUnitsSetStyle[]
NaturalConvert[ly^2,m^2]

Data structures

Most of the data structures used by the package are visible to the user. The most important is $UnitRules, which contains all unit conversion rules in their base form. This can be modified by hand, but NaturalUnitsSetup[] must be called afterwards to update the changes everywhere (NaturalUnitsAdd does this automatically). $PrefixedUnitRules is a version of $UnitRules with prefixed units added.

$UnitRules

$AllUnits is a list of all defined units, whereas $BasicUnits contains the base (SI or equivalent) units, and $NatUnits contains the natural base units of the current unit system. These shouldn’t be changed by hand.

NaturalUnitsSetup[NewUnitSystem->CosmologyUnits]
$AllUnits
$BasicUnits
$NatUnits

$ToNatUnits is the main conversion table, which can be used as-is for simple conversions to the current natural units. $FromBasicToNatUnits does the same but only for the basic units.

$ToNatUnits

ly/.$ToNatUnits

Physics-motivated examples

The true power of the package is in making order-of-magnitude estimates based on physics formulas presented in a natural units form or lacking full dimensional information. Some cosmology motivated examples are shown below.

NaturalUnitsSetup[NewUnitSystem->CosmologyUnits]

Rough wavelength of cosmic microwave background (CMB) photons, starting form the temperature:

NaturalConvert[1/(2.7K),mm]

Energy density fraction of photons in the universe today, starting from the CMB temperature and the Hubble parameter:

NaturalConvert[Pi^2/15*(2.7 K)^4/(3 (70 km/s/Mpc)^2*MP^2)]

Radius of an Earth mass black hole:

NaturalConvert[2*MEarth*G,mm]

Number density of solar mass black holes near the Solar System if they make up all dark matter, starting from the local dark matter density 0.3 GeV/cm3:

NaturalConvert[0.3 GeV/cm^3/MSolar,1/pc^3]